import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';
import 'package:sports_new/resources/colours.dart';
import 'package:sports_new/server/api/expert_api.dart';
import 'package:sports_new/utils/date_time_utils.dart';
import 'package:sports_new/utils/routes.dart';
import 'package:sports_new/utils/utils.dart';
import 'package:sports_new/utils/widget_utils.dart';
import 'package:visibility_detector/visibility_detector.dart';
import '../../../entity/home/subs_list_entity.dart';
import '../../../widgets/base/qkd_text.dart';
import '../../../widgets/card.dart';

class SubsBar extends StatefulWidget {
  const SubsBar({super.key, required this.expertId, this.padding});

  final String expertId;
  final EdgeInsets? padding;

  @override
  State<SubsBar> createState() => _SubsBarState();
}

class _SubsBarState extends State<SubsBar> {
  SubsListEntity? data;
  int? roomId = Get.parameters['roomId'].toInt();

  @override
  void initState() {
    getData();
    super.initState();
  }

  Future getData() async{
    data = await ExpertApi.getSubsDetail(widget.expertId,roomId: roomId);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    if (data == null) {
      return Container();
    } else {
      return VisibilityDetector(
        key: const Key("subsBar"),
        onVisibilityChanged: (info){
          if(!info.visibleBounds.isEmpty){
            getData();
          }
        },
        child: Container(
          clipBehavior: Clip.antiAlias,
          height: 66,
          padding: const EdgeInsets.symmetric(horizontal: 16),
          decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(8),
              image: DecorationImage(
                  image: AssetImage(Utils.getImgPath("subs_bar_back.png")),
                  isAntiAlias:true,
                  fit: BoxFit.cover
              )
          ),
          child: Row(
            children: [
              Expanded(
                child: Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Flexible(
                      child: QkdText(
                        Get.currentRoute.contains(Routes.userPage)?
                        "订阅专家，解锁所有方案（${data?.subsPrices?.first.needPrices}/周）":
                        "订阅专家，解锁所有方案\n${data?.subsPrices?.first.needPrices}/周",
                        fontSize: 13,
                        color: Colours.goldFFEEC8,
                        fontWeight: FontWeight.w500,
                        height: 1.8
                      )
                    ),
                    if(data?.endTime.hasValue == true)
                      QkdText(
                        "有效期：${DateTime.parse(
                          data?.endTime ?? ""
                        ).formatedString("yyyy-MM-dd")}",
                        fontSize: 10,color: const Color(0xFFC7BA9E),height: 1.4
                      )
                  ],
                ),
              ),
              // Container(width: 4),
              Container(
                width: 72,height: 32,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    gradient: const LinearGradient(
                        colors: [Color(0xFFFFE1A0),Color(0xFFFFB443)],
                        begin: Alignment.centerLeft,end: Alignment.centerRight
                    ),
                    borderRadius: BorderRadius.circular(30)
                ),
                child: QkdText(
                  data?.endTime != null?"立即续费":"立即订阅",
                  fontSize: 13,
                  fontWeight: FontWeight.w500,
                  color: Colours.brown493B31,
                ),
              ).tap(() {
                if(Get.currentRoute.contains(Routes.userPage)) {
                  Utils.onEvent('expert_detail_subscribe');
                }else if(Get.currentRoute.contains(Routes.viewpoint)){
                  Utils.onEvent('plan_detail_expert_subscribe', params: {'expert_id': widget.expertId, 'param2': roomId == null ? '0' : '1'});
                }
                  Get.toNamed(
                    Routes.subsPay,
                    arguments: widget.expertId,
                    parameters: {"roomId":"${roomId??""}"}
                  );
                }
              )
            ],
          )
        ),
      );
    }
  }

  Widget textBlock(String title,String content){
    return Column(
      children: [
        QkdText(content,color: Colours.goldFFEEC8),
        QkdText(title,
          fontSize: 10,color: Colours.brownC7BA9E
        )
      ],
    );
  }
}
